package com.efast.dmom.mes.service.impl;

import java.time.LocalDate;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.efast.dmom.mes.bean.Plan;
import com.efast.dmom.mes.bean.ResultBean;
import com.efast.dmom.mes.datahandlers.entity.PlanDataHandler;
import com.efast.dmom.mes.entity.PlanEntity;
import com.efast.dmom.mes.service.PlanService;

@Service
public class PlanServiceImpl implements PlanService {

	@Autowired
	private PlanDataHandler planDataHandler;

	@Override
	public ResultBean save(Plan plan) {
		ResultBean resultBean = new ResultBean(true, ' ');
		PlanEntity planEntity = plan.getInnerData();
		if (planEntity == null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("保存的信息为空！");
			return resultBean;
		}
		if (planEntity.getPartId() == 0) {
			resultBean.setSuccess(false);
			resultBean.setMessage("请检查产品编号和产品名称是否选择！");
			return resultBean;
		}
		if (planEntity.getCustomerId() == 0) {
			resultBean.setSuccess(false);
			resultBean.setMessage("请检查客户是否选择！!");
			return resultBean;
		}
		if (planEntity.getQuantity() == 0) {
			resultBean.setSuccess(false);
			resultBean.setMessage("计划数量不能等于0！");
			return resultBean;
		}
		if (planEntity.getOfflineDate() != null) {
			LocalDate offlineDate = planEntity.getOfflineDate();
			LocalDate now = LocalDate.now();
			int i = offlineDate.compareTo(now);
			if (i < 0) {
				resultBean.setSuccess(false);
				resultBean.setMessage("计划的交付日期不能早于今天！");
				return resultBean;
			}
		}
		return planDataHandler.save(plan);
	}

	@Override
	public ResultBean update(Plan plan) {
		ResultBean resultBean = new ResultBean(true, ' ');
		PlanEntity planEntity = plan.getInnerData();
		if (planEntity == null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("更新的信息为空！");
			return resultBean;
		}
		if (planEntity.getPartId() == 0) {
			resultBean.setSuccess(false);
			resultBean.setMessage("请检查产品编号和产品名称是否选择！");
			return resultBean;
		}
		if (planEntity.getCustomerId() == 0) {
			resultBean.setSuccess(false);
			resultBean.setMessage("请检查客户是否选择！!");
			return resultBean;
		}
		if (planEntity.getQuantity() == 0) {
			resultBean.setSuccess(false);
			resultBean.setMessage("计划数量不能等于0！");
			return resultBean;
		}
		if (planEntity.getOfflineDate() != null) {
			LocalDate offlineDate = planEntity.getOfflineDate();
			LocalDate now = LocalDate.now();
			int i = offlineDate.compareTo(now);
			if (i < 0) {
				resultBean.setSuccess(false);
				resultBean.setMessage("计划的交付日期不能早于今天！");
				return resultBean;
			}
		}
		return planDataHandler.update(plan);
	}

	@Override
	public ResultBean delete(Plan plan) {
		ResultBean resultBean = new ResultBean(true, ' ');
		PlanEntity planEntity = plan.getInnerData();
		if (planEntity == null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("删除的信息为空！");
			return resultBean;
		}
		return planDataHandler.delete(plan);
	}

	@Override
	public ResultBean closePlan(Plan plan) {
		ResultBean resultBean = new ResultBean(true,' ');
		PlanEntity planEntity = plan.getInnerData();
		if (planEntity == null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("关闭的信息为空！");
			return resultBean;
		}
		return planDataHandler.closePlan(plan);
	}

}
